package XMFramework.Core
{
	import mx.utils.StringUtil;

	public class SError extends Error
	{
		// Defined error type for error report system
		public static const ERRTYPE_LOGIC_ERROR : int		=	1;
		public static const ERRTYPE_APP_CRASH : int			=	2;
		public static const ERRTYPE_SERVER_ERROR : int		=	3;
		
		// Error code		
		public static const ERR_MALFUNCTION : int			=	1;
		public static const ERR_INTERNAL : int				=	2;
		public static const ERR_SERVER_ERROR : int			=	3;
		
		/////////////////////// 
		// Application error				
		public static const ERR_NETWORK_CONNECT : int		=	1001;
		public static const ERR_JSON_NOT_FOUND : int		=	1002;
		public static const ERR_LOCALIZATION_NOT_FOUND : int	=	1003;
		public static const ERR_RES_LOAD_FAILED : int		=	1004;
		public static const ERR_AFM_RESPONSE_FAULT : int	=	1005;
		public static const ERR_PAYMENT_CONNECT : int		=	1006;
		public static const ERR_NETWORK_FAILED : int		=	1007;
		
		//////////////////////////////////////////////////////
		// Game data error
		public static const ERR_OUT_OF_EXP_TREE : int		=	5001;
		public static const ERR_OUT_OF_ENERGY_TREE : int	=	5002;
		public static const ERR_NO_ACTIVE_QUEST : int		=	5003;
		public static const ERR_RECOVERY_QUEST_FAILED : int	=	5004;
		public static const ERR_OUT_OF_BLOCK_COST : int		=	5005;
		public static const ERR_ENTITY_CONF_NOT_FOUND : int	=	5006;
		public static const ERR_PROPERTIES_CONF_NOT_FOUND : int		=	5007;
		public static const ERR_CB_VER_RESTRICTION : int		=	5008;
		public static const ERR_OUT_OF_FRIENDSHIP_TREE : int	=	5009;
		
		//////////////////////////////////////////////////////
		// Error message define
		public static const ErrorMessage : Object			=	new Object();
		public static const ParamsHandle : Object			=	new Object();
		
		//////////////////////////////////////////////////////
		ErrorMessage[SError.ERR_MALFUNCTION]				=	"Malfunction: This feature is on development.";
		ParamsHandle[SError.ERR_MALFUNCTION]				=	null;
		
		ErrorMessage[SError.ERR_INTERNAL]					=	"Internal Error: {0}";
		ParamsHandle[SError.ERR_INTERNAL]					=	parserParams_1;
		
		ErrorMessage[SError.ERR_SERVER_ERROR]				=	"Server Error: {0}";
		ParamsHandle[SError.ERR_SERVER_ERROR]				=	parserParams_1;
		
		// Application error
		ErrorMessage[SError.ERR_NETWORK_CONNECT]			=	"Error connection [BadVersion]. At {0}";
		ParamsHandle[SError.ERR_NETWORK_CONNECT]			=	parserParams_1;
		
		ErrorMessage[SError.ERR_NETWORK_FAILED]				=	"Error connection [Failed]. At {0}";
		ParamsHandle[SError.ERR_NETWORK_FAILED]				=	parserParams_1;
		
		ErrorMessage[SError.ERR_PAYMENT_CONNECT]			=	"Can not connect to payment. At {0}";
		ParamsHandle[SError.ERR_PAYMENT_CONNECT]			=	parserParams_1;
		
		ErrorMessage[SError.ERR_JSON_NOT_FOUND]				=	"JSON configuration data file not found. At {0}";
		ParamsHandle[SError.ERR_JSON_NOT_FOUND]				=	parserParams_1;
		
		ErrorMessage[SError.ERR_LOCALIZATION_NOT_FOUND]		=	"Localization data file not found. At {0} for '{1}'";
		ParamsHandle[SError.ERR_LOCALIZATION_NOT_FOUND]		=	parserParams_2;		
		
		ErrorMessage[SError.ERR_RES_LOAD_FAILED]			=	"Resource not found. Priority={0}, swf={1}";
		ParamsHandle[SError.ERR_RES_LOAD_FAILED]			=	parserParams_2;		
		
		ErrorMessage[SError.ERR_AFM_RESPONSE_FAULT]			=	"AMF fault response";
		ParamsHandle[SError.ERR_AFM_RESPONSE_FAULT]			=	parserParams_2;		
		
		
		
		
		
		//////////////////////////////////////////////////////
		// Game data error
		ErrorMessage[SError.ERR_OUT_OF_EXP_TREE]			=	"Level {0} is out of Experiment Tree";
		ParamsHandle[SError.ERR_OUT_OF_EXP_TREE]			=	parserParams_1;
	
		ErrorMessage[SError.ERR_OUT_OF_ENERGY_TREE]			=	"Level {0} is out of Energy Tree";
		ParamsHandle[SError.ERR_OUT_OF_ENERGY_TREE]			=	parserParams_1;
				
		ErrorMessage[SError.ERR_NO_ACTIVE_QUEST]			=	"Can not recovery active quest. Finish quest list not found";
		ParamsHandle[SError.ERR_NO_ACTIVE_QUEST]			=	parserParams_1;	
		
		ErrorMessage[SError.ERR_RECOVERY_QUEST_FAILED]		=	"Can not recovery active quest: {0}";
		ParamsHandle[SError.ERR_RECOVERY_QUEST_FAILED]		=	parserParams_1;	
		
		ErrorMessage[SError.ERR_OUT_OF_BLOCK_COST]			=	"Map block {0} is out of config data";
		ParamsHandle[SError.ERR_OUT_OF_BLOCK_COST]			=	parserParams_1;	
		
		ErrorMessage[SError.ERR_ENTITY_CONF_NOT_FOUND]		=	"Config data not found for entity {0}";
		ParamsHandle[SError.ERR_ENTITY_CONF_NOT_FOUND]		=	parserParams_1;	
		
		ErrorMessage[SError.ERR_PROPERTIES_CONF_NOT_FOUND]	=	"Config data not found for properties {0}";
		ParamsHandle[SError.ERR_PROPERTIES_CONF_NOT_FOUND]	=	parserParams_1;	
		
		ErrorMessage[SError.ERR_CB_VER_RESTRICTION]			=	"Your account is restricted in CB version";
		ParamsHandle[SError.ERR_CB_VER_RESTRICTION]			=	parserParams_1;	
		
		ErrorMessage[SError.ERR_OUT_OF_FRIENDSHIP_TREE]		=	"Level {0} is out of Friendship Tree";
		ParamsHandle[SError.ERR_OUT_OF_FRIENDSHIP_TREE]		=	parserParams_1;
		
		
		/////////////////////////////////////////////////////////
		private var _flashError : *	=	null;
		
		/**
		 * 
		 * @param message
		 * @param id
		 * @param flashErr
		 * 
		 */		
		public function SError( id:int=0, flashErr : * = null, ...params )
		{
			_flashError			=	flashErr;
			
			var msg : String	=	"...";
			if( SError.ErrorMessage.hasOwnProperty( id ) )
			{
				if( SError.ParamsHandle.hasOwnProperty( id ) && SError.ParamsHandle[id] != null )
					msg			=	SError.ParamsHandle[id].apply( null, [SError.ErrorMessage[id]].concat(params) );
				else
					msg			=	SError.ErrorMessage[id];
			}
			
			super( msg, id);
		}
		
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get flashError() : *
		{
			return _flashError;
		}
		
		public function getDetailMessage() : String
		{
			var msg : String	=	super.message;
			if( flashError != null && flashError.hasOwnProperty( "message" ) )
				msg	+=	":\n" + flashError.message;
			
			if( flashError != null && flashError.hasOwnProperty( "text" ) )
				msg	+=	":\n" + flashError.text;
			
			return msg;
		}
		
		//////////////////////////////////////////////////////////////////////
		private static function parserParams_1( msg : String, ...params ) : String
		{
			if( params.hasOwnProperty( "0" ) )
				return StringUtil.substitute( msg + ": \n{0}", params[0] );
			
			return msg;
		}
		
		private static function parserParams_2( msg : String, ...params ) : String
		{
			if( params.hasOwnProperty( "0" ) && params.hasOwnProperty( "1" ) )
				return StringUtil.substitute( msg + ": \n{0}\n{1}", params[0], params[1] );
			
			return msg;
		}
	}
}